package com.bb.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.bb.dao.UserDao;
import com.bb.pojo.User;
import com.bb.service.UserService;
import com.bb.util.GetSqlSessionUtil;

public class UserServiceImpl implements UserService{
	/*查询出所有的用户*/
	public Map getAllUser(Map map){
		/*当前页*/
		int currentPage = (int) map.get("currentPage");
		//获取分页数
		 int rows = (int) map.get("rows");
		int start = (currentPage-1)*rows; //计算开始索引
		int end = currentPage*rows+1; //计算结束索引
		
		HashMap hashMap2 = new HashMap();
		HashMap hashMap = hashMap2;
		Map paramMap = hashMap;
		paramMap.put("start", start);
		paramMap.put("end", end);
		
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		List<User> list = mapper.list(paramMap);
		//总记录数
		int totalRows = mapper.countlist(paramMap);
		Map dataMap = new HashMap();
		dataMap.put("list", list);
		dataMap.put("currentPage", currentPage);
		
		//总页数
		if(totalRows % rows ==0){
			dataMap.put("totalPage", totalRows/rows);
		}else{
			dataMap.put("totalPage", totalRows/rows+1);
		}
		sqlSession.close();
		return dataMap;
	}
	/*登录
	public User chkLogin(String username){
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		User userLogin = mapper.userLogin(username);
		
		return userLogin;
	}*/
	
	@Override
	public User getOneUser(int u_id) {
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		User selectOneuser = mapper.selectOneuser(u_id);
		
		return selectOneuser;
	}
	/*处理用户的修改信息*/
	@Override
	public int updateUser(User user) {
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		int i = mapper.uptUser(user);
		sqlSession.commit();
		
		return i;
	}
	/*处理用户的删除*/
	@Override
	public int delUser(int u_id) {
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		int i = 0;
		i =  mapper.deleUser(u_id);
		sqlSession.commit();
		return i;
	}
	//处理添加数据
	@Override
	public int addUser(User user) {
		SqlSession sqlSession = new GetSqlSessionUtil().getSqlSession();
		UserDao mapper = sqlSession.getMapper(UserDao.class);
		int i = 0;
		i = mapper.insertUser(user);
		sqlSession.commit();
		return i;
	}
	
}
